﻿using System;
using System.Collections.Generic;
using System.Linq;
using WroxBlog.Models.Interfaces;

namespace WroxBlog.Models.Implementations
{
    public class CommentRepository : ICommentRepository
    {
        #region ICommentRepository Members

        public CommentCollection GetComments(int postID)
        {
            WroxBlogDataContext context = new WroxBlogDataContext();

            var selectedPost = (from post in context.Posts
                                where post.ID == postID
                                select post).Single();

            List<Comment> comments = selectedPost.Comments.ToList<Comment>();

            return GetCommentsCollection(comments);
        }

        public void AddComment(Comment comment)
        {
            comment.DateAdded = DateTime.Now;

            WroxBlogDataContext context = new WroxBlogDataContext();
            context.Comments.InsertOnSubmit(comment);

            context.SubmitChanges();
        }

        #endregion

        #region Private Methods

        private CommentCollection GetCommentsCollection(List<Comment> comments)
        {
            CommentCollection collection = new CommentCollection();
            collection.AddRange(comments);
            return collection;
        }

        #endregion
    }
}
